package com.neusoft.elmboot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.neusoft.elmboot.po.dto.UserPointTupleDTO;
import com.neusoft.elmboot.po.point.Point;
import com.neusoft.elmboot.po.point.PointMall;
import com.neusoft.elmboot.po.point.UserPointTuple;
import com.neusoft.elmboot.po.vo.UserPointTupleVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface PointMapper extends BaseMapper<Point> {
    @Update("update elm.point set value = value + #{amount} where id = #{userId}")
    void changePoint(String userId, Integer amount);

    @Select("select id, name, point, amount from elm.pointmall")
    List<PointMall> getPointMallList();

    @Select("select id, name, point, amount from elm.pointmall where id = #{id}")
    PointMall getPointMall(Integer id);

    @Update("update elm.pointmall set amount = amount - #{amount} where id = #{id}")
    void updatePointMall(Integer id, Integer amount);

    @Select("select name, amount, cur_amount from elm.userpointtuple " +
            "where user_id = #{userId} and amount > 0")
    List<UserPointTupleVO> showUserPointTupleList(String userId);

    @Select("select amount, cur_amount from elm.userpointtuple " +
            "where user_id = #{userId} and name = #{name}")
    UserPointTupleDTO getUserPointTuple(String userId, String name);

    @Update("update elm.userpointtuple " +
            "set amount = amount + #{amount}, userpointtuple.cur_amount = cur_amount + #{amount} " +
            "where user_id = #{userId} and name = #{name}")
    void updateUserPointTuple(String userId, String name, Integer amount);

    @Insert("insert into elm.userpointtuple " +
            "(tuple_id, user_id, name, amount, cur_amount) " +
            "value (#{tupleId}, #{userId}, #{name}, #{amount}, #{curAmount})")
    void addUserPointTuple(UserPointTuple tuple);

    @Update("update elm.userpointtuple set amount = 0, cur_amount = 0")
    void resetUserPointTuple();
}
